package com.mango.ch07;

import java.io.IOException;
import java.net.URISyntaxException;

import com.mango.HDFSTools.HDFS_HA_API;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import com.mango.HDFSTools.HDFSAPI;

/**
 * 使用mapreduce框架实现购物篮分析 获得某两个商品一起出现的频度
 *
 * @author Mango
 */
public class MBA_Job {
    static Path inputPath = new Path("/MBA_Job/input");// input
    static Path outPath = new Path("/MBA_Job/output"); // output

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = HDFS_HA_API.getConf();
        HDFS_HA_API hdfs = new HDFS_HA_API();
        hdfs.checkOutputPath(outPath);
        hdfs.checkInputPath(inputPath);
        conf.set("number.of.pairs", "2");
        Job job = Job.getInstance(conf, "MBA_Job");
        job.setJarByClass(MBA_Job.class);
        job.setNumReduceTasks(5);

        job.setMapperClass(MBAMaper.class);
        job.setReducerClass(MBAReducer.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        job.setOutputFormatClass(TextOutputFormat.class);

        FileOutputFormat.setOutputPath(job, outPath);
        FileInputFormat.setInputPaths(job, inputPath);
        boolean status = job.waitForCompletion(true);
        System.exit(status ? 0 : 1);
    }

}
